<template>
  <el-dialog
    :title="!dataFormkan.bid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataFormkan" :rules="dataRulekan" ref="dataFormkan" @keyup.enter.native="dataFormkanSubmit()" label-width="80px">
    <el-form-item label="商品编号" prop="gid">
      <el-input v-model="dataFormkan.gid" placeholder="商品编号"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="gname">
      <el-input v-model="dataFormkan.gname" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="bstartdate">
      <el-input v-model="dataFormkan.bstartdate" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="benddate">
      <el-input v-model="dataFormkan.benddate" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="原价" prop="gprice">
      <el-input v-model="dataFormkan.gprice" placeholder="原价"></el-input>
    </el-form-item>
    <el-form-item label="低价" prop="bjprice">
      <el-input v-model="dataFormkan.bjprice" placeholder="低价"></el-input>
    </el-form-item>
    <el-form-item label="规则" prop="brules">
      <el-input v-model="dataFormkan.brules" placeholder="规则"></el-input>
    </el-form-item>
    <el-form-item label="砍几刀或者能砍多少钱" prop="countorlow">
      <el-input v-model="dataFormkan.countorlow" placeholder="砍几刀或者能砍多少钱"></el-input>
    </el-form-item>
    <el-form-item label="详情描述" prop="detail">
      <el-input v-model="dataFormkan.detail" placeholder="详情描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormkanSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataFormkan: {
          bid: 0,
          gid: '',
          gname: '',
          bstartdate: '',
          benddate: '',
          gprice: '',
          bjprice: '',
          brules: '',
          countorlow: '',
          detail: ''
        },
        dataRulekan: {
          gid: [
            { required: true, message: '商品编号不能为空', trigger: 'blur' }
          ],
          gname: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          bstartdate: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          benddate: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          gprice: [
            { required: true, message: '原价不能为空', trigger: 'blur' }
          ],
          bjprice: [
            { required: true, message: '低价不能为空', trigger: 'blur' }
          ],
          brules: [
            { required: true, message: '规则不能为空', trigger: 'blur' }
          ],
          countorlow: [
            { required: true, message: '砍几刀或者能砍多少钱不能为空', trigger: 'blur' }
          ],
          detail: [
            { required: true, message: '详情描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataFormkan.bid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataFormkan'].resetFields()
          if (this.dataFormkan.bid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargain/info/${this.dataFormkan.bid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataFormkan.gid = data.bargain.gid
                this.dataFormkan.gname = data.bargain.gname
                this.dataFormkan.bstartdate = data.bargain.bstartdate
                this.dataFormkan.benddate = data.bargain.benddate
                this.dataFormkan.gprice = data.bargain.gprice
                this.dataFormkan.bjprice = data.bargain.bjprice
                this.dataFormkan.brules = data.bargain.brules
                this.dataFormkan.countorlow = data.bargain.countorlow
                this.dataFormkan.detail = data.bargain.detail
              }
            })
          }
        })
      },
      // 表单提交
      dataFormkanSubmit () {
        this.$refs['dataFormkan'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bargain/bargain/${!this.dataFormkan.bid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bid': this.dataFormkan.bid || undefined,
                'gid': this.dataFormkan.gid,
                'gname': this.dataFormkan.gname,
                'bstartdate': this.dataFormkan.bstartdate,
                'benddate': this.dataFormkan.benddate,
                'gprice': this.dataFormkan.gprice,
                'bjprice': this.dataFormkan.bjprice,
                'brules': this.dataFormkan.brules,
                'countorlow': this.dataFormkan.countorlow,
                'detail': this.dataFormkan.detail
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
